package com.x.erp.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.x.erp.model.Market;
import com.x.erp.rpc.MarketRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

/**
 * 市场管理Controller Created by gdf on 2018/1/19.
 */
@RestController
public class MarketController extends BaseController {

	@Reference
	private MarketRpcService marketRpcService;

	@Autowired
	private FastFileStorageClient fdfsClient;// 文件服务

	/**
	 * 新增或修改市场
	 * 
	 * @param market
	 * @param file
	 * @return
	 */
	@RequestMapping(value = "/insertOrUpdateMarket", method = RequestMethod.POST)
	public ResultModel insertOrUpdateMarket(Market market, MultipartFile file) {

		String fdfsId = null;
		if (file != null && !file.isEmpty()) {
			String delFileId = market.getMarketLogo();
			// 上传图片
			try {
				fdfsId = fdfsClient.uploadFile(file.getInputStream(), "png");
				if (fdfsId == null) {
					return new ResultModel(ResultCode.RESULT_FAIL, "Logo上传服务器失败，请重新提交数据！");
				}
				market.setMarketLogo(fdfsId);
			} catch (Exception e) {
				e.printStackTrace();
				return new ResultModel(ResultCode.RESULT_FAIL, "Logo上传服务器失败，请重新提交数据！");
			}
			// 删除原有图片
			if (StringUtils.isNotEmpty(delFileId)) {
				try {
					fdfsClient.deleteFile(delFileId);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		logger.info("fdfsId =======" + fdfsId);
		if (this.isNotNull(market.getMarketId())) {
			marketRpcService.updateMarket(market);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "修改成功！");
		} else {
			market.setMarketLogo(fdfsId);
			marketRpcService.insertMarket(market);
			return new ResultModel(ResultCode.RESULT_SUCCESS, "新增成功！");
		}
	}

	/**
	 * 查询市场列表
	 * 
	 * @param market
	 * @return
	 */
	@RequestMapping(value = "/getMarketList", method = { RequestMethod.GET, RequestMethod.POST })
	public ResultModel getMarketList(Market market) {
		if (StringUtils.isEmpty(market.getMarketId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "市场ID不能为空！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", marketRpcService.selectMarketList(market));
	}


	/**
	 * 查询市场NUM
	 *
	 * @param market
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getMarketNum", method = {RequestMethod.GET, RequestMethod.POST})
	public ResultModel getMarketNum(Market market) {
		if (StringUtils.isEmpty(market.getMarketId())) {
			return new ResultModel(ResultCode.RESULT_FAIL, "marketId不能为空！");
		}
		Map<String, Object> res = new HashMap<>();
		res.put("total", marketRpcService.selectMarketNum(market));
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", res);
	}


}

